package com.flx.noodle.websocket.handshake;


import com.flx.noodle.websocket.entity.SSOUser;
import com.flx.noodle.websocket.security.SignInterceptor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
import util.CookieUtils;
import util.WsConstant;

import java.util.List;
import java.util.Map;

/**
 * Created by fenglixiong on 2017/10/2.
 * 创建握手
 */
@Component
public class ChatInterceptor extends HttpSessionHandshakeInterceptor{

    protected final Log logger = LogFactory.getLog(getClass());

    @Autowired
    private SignInterceptor signInterceptor;

    /**
     * 握手前
     */
    @Override
    public boolean beforeHandshake(ServerHttpRequest request,
                                   ServerHttpResponse response, WebSocketHandler wsHandler,
                                   Map<String, Object> attributes) throws Exception {
        System.out.println(">>>>>>>>>>>Before Handshake");
        super.beforeHandshake(request, response, wsHandler, attributes);
        String cookieValue = this.getCookieValue(request,WsConstant.SESSION_COOKIE_KEY);
        String session_id_cookie = this.signInterceptor.getSessionIdFromCookie(cookieValue);
        if(session_id_cookie!=null){
            SSOUser ssoUser = this.signInterceptor.remoteCheckSessionBySessionId(session_id_cookie);
            if (ssoUser != null) {
                attributes.put(WsConstant.WEBSOCKET_SESSION_KEY,ssoUser);
                logger.info("成功从cookie中得到用户信息！ws连接中...");
                return true;
            }else {
                logger.info("没有查到用户信息，禁止用户连接websocket！！！");
                return false;
            }
        }else {
            logger.info("未从cookie中获得sessionId！！！禁止连接ws！！！");
            return false;
        }

    }

    private String getCookieValue(ServerHttpRequest request, String key) {
        List cookie_list = request.getHeaders().get("COOKIE");
        if (cookie_list != null && cookie_list.size() > 0) {
            String cookies = (String) cookie_list.get(0);
            logger.info(cookies);
            if (cookies != null) {
                String session_cookie = CookieUtils.getCookieValue(cookies, key);
                return session_cookie;
            }
        }

        return null;
    }

    /**
     * 握手后
     */
    @Override
    public void afterHandshake(ServerHttpRequest request,
                               ServerHttpResponse response, WebSocketHandler wsHandler,
                               Exception ex) {
        System.out.println(">>>>>>>>>>>After Handshake");
        super.afterHandshake(request, response, wsHandler, ex);
    }

}
